package com.itbaizhan.shopping_cart_customer_api.controller;

/**
 * @author spf
 * @date 2024/1/17
 * @time 16:35
 */

import com.itbaizhan.shopping_common.pojo.CartGoods;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.CartService;
import com.itbaizhan.shopping_common.utils.JWTUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车控制器API
 */
@RestController
@RequestMapping("/user/cart")
public class CartController {
    @Reference
    private CartService cartService;

    /**
     * 查询用户购物车列表
     * @param token 用户令牌
     * @return 用户购物车列表
     */
    @GetMapping("/findCartList")
    public BaseResult<List<CartGoods>> findCartList(@RequestHeader String token){
        Long userId = JWTUtil.getId(token);
        List<CartGoods> cartList = cartService.findCartList(userId);
        return BaseResult.ok(cartList);
    }

    /**
     * 添加购物车
     * @param token 令牌
     * @param cartGoods 购物查商品
     * @return 执行结果
     */
    @PostMapping("/addCart")
    public BaseResult addCart(@RequestHeader String token, @RequestBody CartGoods cartGoods){
        Long userId = JWTUtil.getId(token);
        cartService.addCart(userId, cartGoods);
        return BaseResult.ok();
    }

    /**
     * 修改购物车商品数量
     * @param token 令牌
     * @param goodId 商品id
     * @param num 数量
     * @return 执行结果
     */
    @GetMapping ("/handleCart")
    public BaseResult handleCart(@RequestHeader String token, Long goodId, Integer num){
        Long userId = JWTUtil.getId(token);
        cartService.handleCart(userId, goodId, num);
        return BaseResult.ok();
    }

    /**
     * 删除购物车商品
     * @param token 令牌
     * @param goodId 商品id
     * @return 执行结果
     */
    @DeleteMapping("/deleteCart")
    public BaseResult deleteCart(@RequestHeader String token, Long goodId){
        Long userId = JWTUtil.getId(token);
        cartService.deleteCartOption(userId, goodId);
        return BaseResult.ok();
    }

    /**
     * 测试功能
     * @return 执行结果
     */
    @GetMapping("/test")
    public BaseResult<Long> test(){
        Long id = JWTUtil.getId("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJzcGYiLCJpc3MiOiJCQUlaSEFOIiwiZXhwIjoxNzA1NDg3Njk1LCJpYXQiOjE3MDU0ODYyNTUsInVzZXJJZCI6Mjh9.aL75n7vUIni-B3eVRRz58wd4ECBBzQwhqFNc5yAb1kA");
//        Long id = 28L;
        return BaseResult.ok(id);
    }
}
